package com.gengzp.common.utils;

import com.gengzp.common.exception.BusinessException;

/**
 * @ClassName RandomUtils
 * @Description 随机值工具类
 * @Author gengzp
 * @Date 2025/5/11 4:07
 */
public class RandomUtils {

    private RandomUtils() {
    }

    /**
     * 生成指定长度的随机字符串
     *
     * @param length 指定长度, 最长为32
     */
    public static String createRandomString(int length) {
        if (length < 1 || length > 32) {
            throw BusinessException.get("随机字符串长度超出范围");
        }

        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = Math.abs((int) (Math.random() * str.length()));
            char c = str.charAt(index);
            sb.append(c);
        }
        return sb.toString();
    }

}
